import media from '@blueos.media.audio.mediaManager'
import audio from '@blueos.media.audio.audioPlayer'
import request from '@blueos.network.request'
import prompt from '@blueos.window.prompt'

  // 调用决策询问录音文件
export const playAudio = (path)=>{
  // let newAudio = media.createAudioPlayer();
  // newAudio.src = path;
  // newAudio.play();
  audio.src = path
  audio.play()
}


// 上传录音文件
export const uploadRecord=()=>{
  request.upload({
    url: 'http://172.19.241.49:8080/file/upload',
    files: [
      {
        uri: 'internal://files/madaochenggong/demo.txt',
        name: 'file',
        filename: 'demo.txt',
      },
    ],
    data: [
    ],
    success: function (code,data,headers) {
      console.log(`文件上传成功.`)
      prompt.showToast({
        message:`文件上传成功. data=${data}, headers=${headers}`,
        duration:1
      })
    },
    fail: function ( data, code) {
      console.log(`文件上传失败, data = ${data}, code = ${code}`)
      prompt.showToast({
        message:`文件上传失败,data = ${data}, code = ${code}`,
      })
    },
  })
}

// 录音
export const startRecord=(record)=>{
  record.start({
    // uri:'internal://files/madaochenggong/test.mp3',
    success: function (data) {
      prompt.showToast({
        message:`开始 record, uri=${String(data.uri)}`
      })
    },
    fail: function (data, code) {
      console.log(`录音失败, code = ${code}, errorMsg=${data}`)
    },
    complete:function(){
    }
  })
}

export const endRecord=(record)=>{
  record.stop();
  console.log('录音成功')
  prompt.showToast({
    message:"录音成功"
  })
  uploadRecord();
}
